package com.fiberhome.ztelauncher.core.page;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.fiberhome.zte_launcher.R;
import com.fiberhome.ztelauncher.core.page.model.BaseType;
import com.fiberhome.ztelauncher.uilib.EnterAnimLayout;
import com.fiberhome.ztelauncher.util.BaiYeAnim;
import com.fiberhome.ztelauncher.util.BitmapUitl;
import com.fiberhome.ztelauncher.util.DisplayUtil;
import com.fiberhome.ztelauncher.util.TextStyleUtil;
import com.fiberhome.ztelauncher.xml.bean.LaunchDataPage;

import java.util.List;

/**
 * 动画控件
 * Created by xc on 2018/4/3.
 */

public class SwitchAnimView extends FrameLayout {
    private Context context;
    private EnterAnimLayout enteranimlayout;
    private ImageView ivFirst;
    private ImageView ivSecond;
    private TextView tvName;
    private List<LaunchDataPage.ElementData> listElementData;
    private int index;
    private int transition;
    private int duration;
    private final static int DEFAULT_ANIM = 0;
    private final static int PLATE_ANIM = 1;
    private final static int BAIYE_ANIM = 2;
    private final static int ZOOM_ANIM = 3;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DEFAULT_ANIM:
                    startDeafultAnim();
                    handler.sendEmptyMessageDelayed(DEFAULT_ANIM, duration);
                    break;
                case PLATE_ANIM:
                    startPlateAnim();
                    handler.sendEmptyMessageDelayed(PLATE_ANIM, duration);
                    break;
                case BAIYE_ANIM:
                    startBaiYeAnim();
                    handler.sendEmptyMessageDelayed(BAIYE_ANIM, duration);
                    break;
                case ZOOM_ANIM:
                    startZoomAnim();
                    handler.sendEmptyMessageDelayed(ZOOM_ANIM, duration);
                    break;
                default:
                    break;
            }
        }
    };

    public SwitchAnimView( Context context) {
        super(context);
        init(context);
    }

    public SwitchAnimView( Context context,  AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SwitchAnimView( Context context,  AttributeSet attrs,  int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        enteranimlayout = new EnterAnimLayout(context);
        ivFirst = new ImageView(context);
        ivSecond = new ImageView(context);
        ivFirst.setScaleType(ImageView.ScaleType.FIT_XY);
        ivSecond.setScaleType(ImageView.ScaleType.FIT_XY);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        lp.gravity = Gravity.CENTER;
        enteranimlayout.setLayoutParams(lp);
        ivFirst.setLayoutParams(lp);
        ivSecond.setLayoutParams(lp);
        addView(enteranimlayout);
        enteranimlayout.addView(ivSecond);
        enteranimlayout.addView(ivFirst);

        tvName = new TextView(context);
        LayoutParams textLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        textLp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
        textLp.bottomMargin = DisplayUtil.px2dip(context, 10);
        tvName.setLayoutParams(textLp);
        tvName.setGravity(Gravity.CENTER);
        addView(tvName);
    }

    public ImageView getIvFirst() {
        return ivFirst;
    }

    public ImageView getIvSecond() {
        return ivSecond;
    }

    public int getIndex() {
        return index;
    }

    public void setElementData(BaseType baseType) {
        listElementData = baseType.getDataElement().elementDatas.elementDatas;
        tvName.setVisibility(View.GONE);

        tvName.setTextColor(Color.parseColor(baseType.getPage().fontColor));
        tvName.setTextSize(TypedValue.COMPLEX_UNIT_PX, baseType.getPage().fontSize);
        tvName.setText(listElementData.get(index).elementName);
        tvName.getPaint().setTypeface(TextStyleUtil.getTextStyle(baseType.getPage().fontStyle));
        BitmapUitl.loadBackground(context, this, listElementData.get(0).backGroundContentURL);
        BitmapUitl.loadBitmap(context, ivFirst, listElementData.get(index).contentURL);

        duration = baseType.getDataElement().autoswitchtime * 1000;
        transition = baseType.getDataElement().transition;
        transition = 2;
        checkTransition(transition);
    }

    private void checkTransition(int transition) {
        if (listElementData.size() > 1) {
            if (transition == 0) {
                handler.sendEmptyMessageDelayed(DEFAULT_ANIM, duration);

            } else if (transition == 1) {
                if (listElementData.size() % 2 != 0) {
                    listElementData.addAll(listElementData);
                }
                handler.sendEmptyMessageDelayed(PLATE_ANIM, duration);

            } else if (transition == 2) {
                handler.sendEmptyMessageDelayed(BAIYE_ANIM, duration);

            } else if (transition == 3) {
                if (listElementData.size() % 2 != 0) {
                    listElementData.addAll(listElementData);
                }
                BitmapUitl.loadBitmap(context, ivSecond, listElementData.get(1).contentURL);
                handler.sendEmptyMessageDelayed(ZOOM_ANIM, duration);

            }
        }
    }

    /**
     * 默认动画切换
     */
    private void startDeafultAnim() {
        index++;
        index = index % listElementData.size();
        tvName.setText(listElementData.get(index).elementName);
        BitmapUitl.loadBitmap(context, ivFirst, listElementData.get(index).contentURL);
    }

    /**
     * 胶卷拉动动画
     */
    private void startPlateAnim() {
        index++;
        index = index % listElementData.size();
        tvName.setText(listElementData.get(index).elementName);
        String imgUrl = listElementData.get(index).contentURL;
        if (index % 2 == 1) {
            BitmapUitl.loadBitmap(context, ivSecond, imgUrl);
            ivFirst.startAnimation(AnimationUtils.loadAnimation(context, R.anim.translate_out_left));
            ivSecond.startAnimation(AnimationUtils.loadAnimation(context, R.anim.translate_in_right));
            ivFirst.setVisibility(View.GONE);
            ivSecond.setVisibility(View.VISIBLE);

        } else {
            BitmapUitl.loadBitmap(context, ivFirst, imgUrl);
            ivFirst.startAnimation(AnimationUtils.loadAnimation(context, R.anim.translate_in_right));
            ivSecond.startAnimation(AnimationUtils.loadAnimation(context, R.anim.translate_out_left));
            ivFirst.setVisibility(View.VISIBLE);
            ivSecond.setVisibility(View.GONE);
        }
    }

    /**
     * 百叶窗动画
     */
    private void startBaiYeAnim() {
        index++;
        index = index % listElementData.size();
        tvName.setText(listElementData.get(index).elementName);
        BitmapUitl.loadBitmap(context, ivFirst, listElementData.get(index).contentURL);
        BaiYeAnim anim = new BaiYeAnim(enteranimlayout);
        anim.startAnimation(1000);
    }

    /**
     * 缩放动画
     */
    private void startZoomAnim() {
        index++;
        index = index % listElementData.size();
        tvName.setText(listElementData.get(index).elementName);

        if (index % 2 == 1) {
            BitmapUitl.loadBitmap(context, ivSecond, listElementData.get(index).contentURL);
            ivFirst.bringToFront();
            ivFirst.startAnimation(AnimationUtils.loadAnimation(context, R.anim.scale_small));
            ivSecond.clearAnimation();

        } else {
            BitmapUitl.loadBitmap(context, ivFirst, listElementData.get(index).contentURL);
            ivSecond.bringToFront();
            ivSecond.startAnimation(AnimationUtils.loadAnimation(context, R.anim.scale_small));
            ivFirst.clearAnimation();
        }
    }
}
